﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using WeatherProviders;

namespace OpenWeatherMap.API
{
    class ApiClient
    {
        private const string _baseUrl = "http://openweathermap.org/data/2.1/";
        private readonly JavaScriptSerializer _jsonSerializer = new JavaScriptSerializer();

        public List<Location> FindLocation(String query)
        {
            string responseJson = Helper.GetRequest(new Uri(_baseUrl + "find/name?q=" + query), Encoding.UTF8, 15000);
            var responseObj = _jsonSerializer.Deserialize<FindName>(responseJson);
            return responseObj.list.ToList();
        }
        public Location CurentWeather(Int32 ID)
        {
            string responseJson = Helper.GetRequest(new Uri(_baseUrl + "weather/city/" + ID), Encoding.UTF8, 15000);
            var responseObj = _jsonSerializer.Deserialize<Location>(responseJson);
            return responseObj;
        }
        public List<Location> ForecastWeather(Int32 ID)
        {
            string responseJson = Helper.GetRequest(new Uri(_baseUrl + "forecast/city/" + ID), Encoding.UTF8, 15000);
            var responseObj = _jsonSerializer.Deserialize<FindName>(responseJson);
            return responseObj.list.ToList();
        }
        //view-source:http://openweathermap.org/data/2.1/weather/city/524901
        //view-source:http://openweathermap.org/data/2.1/forecast/city/524901
    }
}
